from datetime import datetime, date, timedelta
import time


def ts_to_time_str(ts=None, time_format='%y%m%d%H%M%S'):
    time_arr = time.localtime(ts)
    return time.strftime(time_format, time_arr)


def mts_to_time_str(time_format='%y%m%d%H%M%S%f'):
    dt = datetime.now()
    return dt.strftime(time_format)


def time_str_to_ts(time_str, time_format='%Y-%m-%d'):
    ts = 0
    try:
        date_struct_time = time.strptime(time_str, time_format)
        ts = int(time.mktime(date_struct_time))
    except Exception as e:
        print(e)
    return ts


def date_str(offset=None, time_format='%Y-%m-%d'):
    if offset is None:
        offset = {
            'days': -1
        }
    _date_str = ''
    try:
        _date_str = (date.today() + timedelta(**offset)).strftime(time_format)
    except Exception as e:
        print(e)
    return _date_str


def date_ts(offset=None, time_format='%Y-%m-%d'):
    _date_ts = 0
    _date_str = date_str(offset, time_format)
    if _date_str != '':
        _date_ts = time_str_to_ts(_date_str, time_format)
    return _date_ts
